const moment = require('moment');

/**
 * @desc 获取时间段 (豪秒)
 * @param start 开始时间
 * @param end 结束时间
 * @param interval 间隔时间
 */
export function getSegment(start: number, end: number, interval: number) {
  const n = (end - start) / interval
  return new Array(n + 1).fill(0).map((it, index) => {
    return {
      time: start + index * interval,
      fmtTime: moment((start + index * interval)).format('YYYY-MM-DD HH:mm:ss')
    }
  })
}

/**
 * format metric (豪秒)
 * @param start
 * @param end
 * @param interval
 * @param metrics
 */
export function getDateValue(start: number, end: number, interval: number, metrics: any[]) {
  let dateList = getSegment(start, end, interval);
  // console.log(start, end, interval)

  metrics = metrics.map((it: any) => {
    return {
      ...it,
      time: Number(it.time)
    }
  })

  let valueList = dateList.map((it: any, index: number) => {
    if (index === 0) {
      return 0
    } else {
      return metrics.filter(m => {
        return (m.time <= it.time) && (m.time > dateList[index - 1].time)
      }).length
    }
  })

  return {
    dateList,
    valueList
  }
}